package com.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author:wesley
 */
public class HDFSTest {

    private FileSystem fileSystem = null;

    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        Configuration configuration = new Configuration();
        String hdfsUrl = "hdfs://node1:9000";
        fileSystem = FileSystem.get(new URI(hdfsUrl), configuration, "hadoop");
    }


    /**
     * 上传文件到hdfs
     *
     * @throws IOException
     */
    @Test
    public void testAddFile() throws IOException {
        // 要上传的文件所在的本地路径
        Path src = new Path("D:\\mybatis-generator.xml");
        // 要上传到hdfs的目标路径
        Path dst = new Path("/");
        fileSystem.copyFromLocalFile(src, dst);
        fileSystem.close();
    }


    /**
     * 下载文件到本地
     * @throws IOException
     */
    @Test
    public void testDownFile() throws IOException {
        fileSystem.copyToLocalFile(new Path("/mybatis-generator.xml"), new Path("e:\\"));
        fileSystem.close();
    }


    /**
     * 删除文件夹
     *
     * @throws IOException
     */
    @Test
    public void testDir() throws IOException {
        Path path = new Path("/test");
        boolean exists = fileSystem.exists(path);
        if (!exists) {
            fileSystem.mkdirs(path);
        } else {
            fileSystem.delete(path, true);
        }
    }

    @Test
    public void testlistFile() throws IOException {
        RemoteIterator<LocatedFileStatus> interator = fileSystem.listFiles(new Path("/"), true);
        while (interator.hasNext()) {
            LocatedFileStatus fileStatus = interator.next();

            System.out.println(fileStatus.getPath().getName());
            System.out.println(fileStatus.getBlockSize());
            System.out.println(fileStatus.getPermission());
            System.out.println(fileStatus.getLen());
        }
    }



}
